package au.com.bus4u.controller.admin;


import au.com.bus4u.controller.BaseController;
import au.com.bus4u.service.MenuService;
import au.com.bus4u.utils.JsonResult;
import au.com.bus4u.utils.PageData;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.extern.java.Log;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.lang.model.SourceVersion;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/menu")
@Log
public class MenuController extends BaseController {
    @Autowired
    private HttpServletRequest request;
    @Resource
    private MenuService menuService;


    /**
     * /admin/menu/add
     */
    @RequiresPermissions("menu:add")
    @ResponseBody
    @RequestMapping({"/add","/addMenu"})
    public JsonResult add() throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        menuService.save(getPageData());
        result.setCode(200);
        result.setMsg("添加成功");
        return result;
    }

    @RequiresPermissions("menu:delete")
    @ResponseBody
    @RequestMapping({"/del","/deleteMenu"})
    public JsonResult deleteMenu(Integer menu_id) throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();

        //删除菜单之前需要判断是否有角色使用了此菜单


        menuService.delete(menu_id);
        result.setCode(200);
        result.setMsg("删除成功");
        return result;
    }

    @RequiresPermissions("menu:update")
    @ResponseBody
    @RequestMapping({"/update","/updateMenu"})
    public JsonResult updateMenu(Integer menu_id) throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        menuService.edit(getPageData());
        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }
    /**
     * 加载菜单管理左边的菜单树的json
     */
    @RequiresPermissions("menu:view")
    @ResponseBody
    @RequestMapping("/loadMenuManagerLeftTreeJson")
    public PageData loadMenuManagerLeftTreeJson() throws Exception {
        log.info("=========================================");
//        System.err.println("MenuController.loadMenuManagerLeftTreeJson");
        PageData result = new PageData();
        result.put("code",0);
        result.put("msg","操作成功");

        List<PageData> dataTree = menuService.loadMenuTree(-1);

//        System.err.println("dataTree = " + dataTree);

        result.put("data", dataTree);

        return result;
    }

    /**
     * 查询
     */
    @RequiresPermissions("menu:view")
    @ResponseBody
    @RequestMapping("loadAllMenu")
    public PageData loadAllMenu() throws Exception {
        log.info("=========================================");
//        System.err.println("MenuController.loadAllMenu");
        setPageHelperStartPage();//从request中获取pageNum和pageSize并设置到PageHelper.startPage

        PageData pageData = getPageData();
        pageData.put("menu_type",1);
        List<PageData> list = menuService.loadAllMenu(pageData); // 列出Category列表
        PageInfo<PageData> pageInfo = new PageInfo<>(list);//得到分页的结果对象
        List<PageData> pageList = pageInfo.getList();//获取返回页面的分页list数据
        PageData page = getPage(pageInfo);//获取分页数据page

        PageData result = new PageData();
        result.put("code",0);
        result.put("data",pageList);
        result.put("page",page);
        result.put("count",page.get("total"));//总条数 layui数据表分页组件需要

        return result;
    }

    /**
     * 查询最大排序号
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("loadMenuMaxSortNum")
    public PageData loadMenuMaxSortNum()  throws Exception {
        log.info("=========================================");
        Integer maxSortNum = menuService.queryMaxSortNum(); // 查询最大排序号
//        System.err.println("maxSortNum = " + maxSortNum);
        PageData result = new PageData();
        result.put("value",maxSortNum);
        return result;
    }
    /**
     * 检查菜单是否包含子菜单
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("checkMenuHasChildrenNode")
    public PageData checkMenuHasChildrenNode(Integer menu_id)  throws Exception {
        log.info("=========================================");

        Integer childrenSize = menuService.checkMenuHasChildrenNode(menu_id); // 查询最大排序号
//        System.err.println("childrenSize = " + childrenSize);
        PageData result = new PageData();
        if(childrenSize != null && childrenSize > 0){
            result.put("value",childrenSize);
        }
        return result;
    }

//    }
}
